import {Component,OnInit,OnDestroy} from '@angular/core';

import { ShopPieService} from './shop.pie.service';

import {Util} from '../../../utils/util';
import {AppSettings} from '../../../app.settings';
import {Scope} from '../../../utils/emit';

@Component({
  selector:'shop-pie',
  templateUrl:'./shop.pie.html',
  providers:[ShopPieService]
})

export class ShopPieComponent{

  shopInfo:any;
  constructor(private service:ShopPieService,private util:Util,private appSettings:AppSettings,private scope:Scope){

    this.shopInfo = this.util.sessionStore().get('shopDetail');

    this.vm.column.fileName = this.shopInfo.shopName+"店铺类目销售占比";

    this.getHttp(this.shopInfo);
  }

  vm = {
    "loading": true,
    "data": [],
    "column":{
      fileName:'',
      dim:['date','uid','categoryId','categoryName','categoryLevel','sellAmount','sellMoney','monthSellAmount','monthSellMoney','productNum'],
      dimText:['日期', '店铺id','类目id','类目', '类目等级','日销售量','日销售额','近30天销售量','近30天销售额','动销宝贝数']
    },
    "type": 'pie-cat'
  };

  columns = ['类目','日销售量','日销售额','近30天销售量','近30天销售额','动销宝贝数'];

  ngOnInit(){
    // 日销，月销通知
    this.scope.on("sellTimeType", (event) => {
      this.vm.loading = true;
      this.getHttp(this.shopInfo);

    });
  }

  ngOnDestroy(){
    if(this.offEvent){
      this.offEvent.unsubscribe();
    }
  }


  export() {
    console.log("export");

    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);


    this.util.export(data, this.vm.column.fileName);
  }


  sellResulet:any = {};
  offEvent:any;
  getHttp(_shop){

    // let json = {
    //   rows:30,
    //   start:0,
    //   uid: _shop.uid,
    //   brandName:'""',
    //   sort: this.appSettings.getSellTimeType() ==="month"?"monthSellMoney":'sellMoney',
    //   sortType:'desc',
    //   leafCategory:1
    // };

    this.sellResulet = {
      'sellMoney':0,
      'sellAmount':0,
      'productNum':0
    };
    var res = {"code":0,"msg":"成功","data":[{"id":"725677994\u000150012478","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012478,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--纸品/湿巾--抽纸","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":136540,"monthSellAmount":4609625,"sellMoney":4345623.92,"monthSellMoney":1.7143286115E8,"productNum":557},{"id":"725677994\u000150018813","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50018813,"categoryName":"童装玩具/孕产/用品--尿片/洗护/喂哺/推车床--纸尿裤/拉拉裤/纸尿片","level1CategoryId":-10101629662731,"categoryLevel":3,"sellAmount":38464,"monthSellAmount":1438128,"sellMoney":4184132.7,"monthSellMoney":1.6733623314E8,"productNum":404},{"id":"725677994\u000150018977","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50018977,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--衣物清洁剂/护理剂--洗衣液","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":90031,"monthSellAmount":3186088,"sellMoney":3901811.95,"monthSellMoney":1.5279810905E8,"productNum":450},{"id":"725677994\u000150012392","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012392,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--乳制品--纯牛奶","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":59161,"monthSellAmount":2514534,"sellMoney":2694625.09,"monthSellMoney":1.2492429607E8,"productNum":370},{"id":"725677994\u000150018994","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50018994,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--纸品/湿巾--卷筒纸","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":75166,"monthSellAmount":2837275,"sellMoney":2704341.66,"monthSellMoney":1.1523798858E8,"productNum":353},{"id":"725677994\u000150466023","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50466023,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--洗发沐浴/个人清洁--头发清洁/护理/造型--洗护套装","level1CategoryId":-10101991348959,"categoryLevel":5,"sellAmount":20706,"monthSellAmount":1064023,"sellMoney":1736505.0,"monthSellMoney":1.035660494E8,"productNum":370},{"id":"725677994\u000150012475","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012475,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--卫生巾/护垫/成人尿裤--卫生巾","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":122370,"monthSellAmount":3411843,"sellMoney":2644797.99,"monthSellMoney":9.469895518E7,"productNum":809},{"id":"725677994\u000150009838","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50009838,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--米/面粉/杂粮--米类--大米","level1CategoryId":-10080634432541,"categoryLevel":5,"sellAmount":39577,"monthSellAmount":1611621,"sellMoney":1788820.53,"monthSellMoney":7.51475195E7,"productNum":306},{"id":"725677994\u000150016427","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50016427,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--乳制品--酸奶","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":20929,"monthSellAmount":1160814,"sellMoney":1243191.7,"monthSellMoney":7.31321905E7,"productNum":108},{"id":"725677994\u0001211104","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":211104,"categoryName":"童装玩具/孕产/用品--奶粉/辅食/营养品/零食--婴幼儿牛奶粉","level1CategoryId":-10101629662731,"categoryLevel":3,"sellAmount":2863,"monthSellAmount":344126,"sellMoney":601499.6,"monthSellMoney":7.249163534E7,"productNum":211},{"id":"725677994\u000150010535","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50010535,"categoryName":"美食/生鲜/零食--零食/坚果/特产--饼干/膨化--膨化食品","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":146841,"monthSellAmount":4101275,"sellMoney":2361571.58,"monthSellMoney":6.955168541E7,"productNum":1265},{"id":"725677994\u0001213202","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":213202,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--洗发沐浴/个人清洁--头发清洁/护理/造型--洗发水","level1CategoryId":-10101991348959,"categoryLevel":5,"sellAmount":26237,"monthSellAmount":1232339,"sellMoney":1353740.08,"monthSellMoney":6.702710438E7,"productNum":324},{"id":"725677994\u000150008144","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008144,"categoryName":"美食/生鲜/零食--酒类--国产白酒","level1CategoryId":-10080634432541,"categoryLevel":3,"sellAmount":7102,"monthSellAmount":187029,"sellMoney":997835.1,"monthSellMoney":6.09058553E7,"productNum":376},{"id":"725677994\u000150012598","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012598,"categoryName":"美食/生鲜/零食--零食/坚果/特产--糕点/点心--西式糕点","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":80707,"monthSellAmount":2309310,"sellMoney":2067319.7,"monthSellMoney":6.042762922E7,"productNum":446},{"id":"725677994\u0001213203","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":213203,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--洗发沐浴/个人清洁--口腔护理--牙膏","level1CategoryId":-10101991348959,"categoryLevel":5,"sellAmount":123242,"monthSellAmount":2429303,"sellMoney":2178557.59,"monthSellMoney":5.93483606E7,"productNum":459},{"id":"725677994\u000150012391","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012391,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--饮料--含乳饮料","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":31786,"monthSellAmount":1515848,"sellMoney":1037123.44,"monthSellMoney":5.831409601E7,"productNum":207},{"id":"725677994\u000150010513","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50010513,"categoryName":"美食/生鲜/零食--零食/坚果/特产--糕点/点心--传统糕点","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":108359,"monthSellAmount":2486300,"sellMoney":2110520.14,"monthSellMoney":5.295956565E7,"productNum":480},{"id":"725677994\u000150008616","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008616,"categoryName":"美食/生鲜/零食--零食/坚果/特产--牛肉干/猪肉脯/卤味零食--牛肉类","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":61400,"monthSellAmount":1877060,"sellMoney":1458441.78,"monthSellMoney":4.592403299E7,"productNum":313},{"id":"725677994\u0001213205","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":213205,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--洗发沐浴/个人清洁--身体清洁--沐浴露","level1CategoryId":-10101991348959,"categoryLevel":5,"sellAmount":25871,"monthSellAmount":1099222,"sellMoney":959495.04,"monthSellMoney":4.363127324E7,"productNum":405},{"id":"725677994\u000150016845","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50016845,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--方便速食--方便面/粉丝/米线","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":97316,"monthSellAmount":2057121,"sellMoney":1325927.67,"monthSellMoney":3.687678611E7,"productNum":767},{"id":"725677994\u000150009866","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50009866,"categoryName":"美食/生鲜/零食--零食/坚果/特产--牛肉干/猪肉脯/卤味零食--猪肉类","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":52058,"monthSellAmount":1545211,"sellMoney":1187683.68,"monthSellMoney":3.61008415E7,"productNum":175},{"id":"725677994\u000150776032","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50776032,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--食用油/调味油--葵花籽油","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":4583,"monthSellAmount":427658,"sellMoney":348746.7,"monthSellMoney":3.2398843E7,"productNum":46},{"id":"725677994\u0001124302001","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":124302001,"categoryName":"美食/生鲜/零食--零食/坚果/特产--饼干/膨化--饼干(新)--曲奇饼干","level1CategoryId":-10080634432541,"categoryLevel":5,"sellAmount":68054,"monthSellAmount":1687325,"sellMoney":1205292.86,"monthSellMoney":3.091063906E7,"productNum":426},{"id":"725677994\u000150010422","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50010422,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--成人奶粉","level1CategoryId":-10080634432541,"categoryLevel":3,"sellAmount":17393,"monthSellAmount":617069,"sellMoney":802149.4,"monthSellMoney":3.04847565E7,"productNum":157},{"id":"725677994\u0001123256001","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":123256001,"categoryName":"美食/生鲜/零食--零食/坚果/特产--豆干制品/蔬菜干--豆腐干","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":53280,"monthSellAmount":1841673,"sellMoney":893074.86,"monthSellMoney":3.016411379E7,"productNum":189},{"id":"725677994\u000150011993","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50011993,"categoryName":"美妆/洗护/医疗--美容护肤/美体/精油--面部护理套装","level1CategoryId":-10081918757497,"categoryLevel":3,"sellAmount":8777,"monthSellAmount":355880,"sellMoney":781964.13,"monthSellMoney":3.002709378E7,"productNum":312},{"id":"725677994\u000150050393","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50050393,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--食用油/调味油--花生油","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":5351,"monthSellAmount":237329,"sellMoney":585399.1,"monthSellMoney":2.96672588E7,"productNum":55},{"id":"725677994\u000150009859","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50009859,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--藕粉/麦片/冲饮品--冲饮麦片","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":19613,"monthSellAmount":766582,"sellMoney":801949.87,"monthSellMoney":2.940323449E7,"productNum":311},{"id":"725677994\u0001121474010","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":121474010,"categoryName":"美妆/洗护/医疗--美容护肤/美体/精油--面膜(新)--面膜","level1CategoryId":-10081918757497,"categoryLevel":4,"sellAmount":7965,"monthSellAmount":357989,"sellMoney":659451.43,"monthSellMoney":2.809613173E7,"productNum":213},{"id":"725677994\u000150013003","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50013003,"categoryName":"美食/生鲜/零食--酒类--葡萄酒","level1CategoryId":-10080634432541,"categoryLevel":3,"sellAmount":8074,"monthSellAmount":213865,"sellMoney":834571.18,"monthSellMoney":2.604982987E7,"productNum":268},{"id":"725677994\u000150050381","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50050381,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--食用油/调味油--调和油","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":15908,"monthSellAmount":451116,"sellMoney":884266.7,"monthSellMoney":2.49116782E7,"productNum":74},{"id":"725677994\u000150002256","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50002256,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--速溶咖啡/咖啡豆/粉--速溶咖啡","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":17399,"monthSellAmount":528877,"sellMoney":633234.42,"monthSellMoney":2.378389136E7,"productNum":266},{"id":"725677994\u000150014008","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50014008,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--家庭环境清洁剂--消毒液","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":14568,"monthSellAmount":465768,"sellMoney":689146.2,"monthSellMoney":2.26405921E7,"productNum":89},{"id":"725677994\u000150008059","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008059,"categoryName":"美食/生鲜/零食--零食/坚果/特产--山核桃/坚果/炒货--其它","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":9455,"monthSellAmount":272467,"sellMoney":743176.7,"monthSellMoney":2.17767958E7,"productNum":45},{"id":"725677994\u000150013099","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50013099,"categoryName":"美食/生鲜/零食--零食/坚果/特产--蜜饯/枣类/梅/果干--枣类制品","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":28801,"monthSellAmount":821228,"sellMoney":757649.28,"monthSellMoney":2.132454033E7,"productNum":226},{"id":"725677994\u000150008056","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008056,"categoryName":"美食/生鲜/零食--零食/坚果/特产--糖果零食/果冻/布丁--糖果","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":42436,"monthSellAmount":1060525,"sellMoney":809899.12,"monthSellMoney":2.128753352E7,"productNum":465},{"id":"725677994\u000150018983","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50018983,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--衣物清洁剂/护理剂--衣物柔顺剂","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":14685,"monthSellAmount":522495,"sellMoney":631803.4,"monthSellMoney":2.12135502E7,"productNum":89},{"id":"725677994\u000150008916","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008916,"categoryName":"美食/生鲜/零食--咖啡/麦片/冲饮--饮料--矿泉水/纯净水","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":19635,"monthSellAmount":540240,"sellMoney":699208.72,"monthSellMoney":2.091852379E7,"productNum":206},{"id":"725677994\u000150008618","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008618,"categoryName":"美食/生鲜/零食--零食/坚果/特产--牛肉干/猪肉脯/卤味零食--鸭肉零食","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":25104,"monthSellAmount":789264,"sellMoney":640412.37,"monthSellMoney":2.030706182E7,"productNum":218},{"id":"725677994\u000150012546","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012546,"categoryName":"童装玩具/孕产/用品--尿片/洗护/喂哺/推车床--湿巾","level1CategoryId":-10101629662731,"categoryLevel":3,"sellAmount":30885,"monthSellAmount":736564,"sellMoney":543290.68,"monthSellMoney":1.94765721E7,"productNum":335},{"id":"725677994\u000150014009","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50014009,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--家庭环境清洁剂--洗洁精","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":33395,"monthSellAmount":840228,"sellMoney":791547.3,"monthSellMoney":1.92254463E7,"productNum":221},{"id":"725677994\u000150009856","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50009856,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--调味品/果酱/沙拉--日常调味品--酱油","level1CategoryId":-10080634432541,"categoryLevel":5,"sellAmount":24090,"monthSellAmount":779896,"sellMoney":483646.79,"monthSellMoney":1.876116909E7,"productNum":226},{"id":"725677994\u0001124304001","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":124304001,"categoryName":"美食/生鲜/零食--零食/坚果/特产--饼干/膨化--饼干(新)--夹心饼干","level1CategoryId":-10080634432541,"categoryLevel":5,"sellAmount":25304,"monthSellAmount":893658,"sellMoney":520181.05,"monthSellMoney":1.761413812E7,"productNum":286},{"id":"725677994\u0001126472004","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":126472004,"categoryName":"美食/生鲜/零食--零食/坚果/特产--山核桃/坚果/炒货--混合坚果","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":8456,"monthSellAmount":209344,"sellMoney":567537.2,"monthSellMoney":1.67580135E7,"productNum":43},{"id":"725677994\u000150012983","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012983,"categoryName":"美食/生鲜/零食--零食/坚果/特产--山核桃/坚果/炒货--长寿果/碧根果","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":21478,"monthSellAmount":626119,"sellMoney":617653.2,"monthSellMoney":1.63420099E7,"productNum":47},{"id":"725677994\u000150050380","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50050380,"categoryName":"美食/生鲜/零食--粮油米面/南北干货/调味品--食用油/调味油--玉米油","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":6598,"monthSellAmount":264853,"sellMoney":434395.18,"monthSellMoney":1.617437421E7,"productNum":60},{"id":"725677994\u0001124478011","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":124478011,"categoryName":"美食/生鲜/零食--零食/坚果/特产--巧克力--夹心巧克力","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":16151,"monthSellAmount":460619,"sellMoney":529286.7,"monthSellMoney":1.59411571E7,"productNum":128},{"id":"725677994\u000150008146","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50008146,"categoryName":"美食/生鲜/零食--酒类--啤酒","level1CategoryId":-10080634432541,"categoryLevel":3,"sellAmount":6097,"monthSellAmount":258562,"sellMoney":346117.2,"monthSellMoney":1.56892871E7,"productNum":239},{"id":"725677994\u000150018976","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50018976,"categoryName":"百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰--衣物清洁剂/护理剂--洗衣粉","level1CategoryId":-10101991348959,"categoryLevel":4,"sellAmount":18444,"monthSellAmount":539967,"sellMoney":484949.56,"monthSellMoney":1.532930838E7,"productNum":130},{"id":"725677994\u000150012987","date":"2017-12-03 00:00:00","uid":725677994,"categoryId":50012987,"categoryName":"美食/生鲜/零食--零食/坚果/特产--山核桃/坚果/炒货--夏威夷果","level1CategoryId":-10080634432541,"categoryLevel":4,"sellAmount":25881,"monthSellAmount":582085,"sellMoney":604275.9,"monthSellMoney":1.51927246E7,"productNum":52}]};
    setTimeout(()=>{

      this.getSellAllResult(this.sellResulet,res.data);

      this.vm.data = res.data;

      this.vm.loading = false;

    },100);


  }

  getSellAllResult(obj,data){

    for(let i in data){
      let item = data[i];

      for(let key in obj){
        obj[key]+=item[key];
      }
    }


  }
}
